package com.decent.common.validator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * 验证工具
 *
 * @author: wangyan
 * @date: 2021/12/16
 */
public class ValidateUtil {

    /**
     * 验证器
     */
    private static final Validator VALIDATOR;

    static {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        VALIDATOR = factory.getValidator();
    }

    /**
     * 验证
     *
     * @param object 校验对象
     * @param groups 分组
     * @param <T>    类型
     * @return 校验结果
     */
    public static <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups) {
        return VALIDATOR.validate(object, groups);
    }

}
